import { dbSet, dbGet, dbClear } from '@/utils/util.db'
import { router } from '@/router'

const actions = {
  set (context, {
    dbName = 'dataBase',
    path = '',
    value = '',
    user = false
  }) {
    dbSet({ dbName, path, value, user })
  },
  get (context, {
    dbName = 'dataBase',
    path = '',
    defaultValue = '',
    user = false
  }) {
    return dbGet({ dbName, path, defaultValue, user })
  },
  clear (context, {
    dbName = 'dataBase',
    path = '',
    user = false
  }) {
    dbSet({ dbName, path, value: {}, user })
  },
  clearAll (context) {
    dbClear()
  },
  pageSet (context, {
    dbName = 'dataBase',
    value = '',
    user = false
  }) {
    const path = router.currentRoute.fullPath
    dbSet({ dbName, path, value, user })
  },
  pageGet(context, {
    dbName = 'dataBase',
    defaultValue = '',
    user = false
  }) {
    const path = router.currentRoute.fullPath
    return dbGet({ dbName, path, defaultValue, user })
  },
  pageClear(context, {
    dbName = 'dataBase',
    user = false
  }) {
    const path = router.currentRoute.fullPath
    const value = {}
    dbSet({ dbName, path, value, user })
  }
}

export default {
  namespaced: true,
  actions
}
